var RoleGroupPage = {
	// oldRoles:null,
	onFlagSelected:function(ele) {
		var flag = $(ele).val();
		this.manageRoleManagementPart(flag);
	},
	// 控制资源管理部分的显示和隐藏，flag=1隐藏，flag=0显示
	manageRoleManagementPart:function(flag) {
		if (flag == '1') {
			$("[data-role-management='true']").hide();
		} else {
			$("[data-role-management='true']").show();
		}
	},
	// 保存资源组
	saveAllRoleGroup:function() {
		var obj = $("#rolegroupBaseInfoFrm").serializeObject();
		if(StringUtil.isEmpty(obj.group_name)){
			$.messager.warn('资源组名称不能为空');return ;
		}
		var ids = new Array();
		var inputs = $("#hasRolesFrm .rolesShowDiv input");
		$.each(inputs,function(index,item){
			ids.push($(this).val());
		});
		$("#groupHasRoles").val(ids);
		$("#rolegroupBaseInfoFrm").submit();
	},
	// 保存资源组成功
	onSaveGroupSucc:function(data){
		if(data.success == true){
			MsgBox.info('保存成功！',null, function() {
				MsgBox.closeActiveDialog();
				gruopInfoXDataTable.refresh();
			});
		}
	},
	onRoleGroupRenderItem: function(colName,text) {
		switch(colName){//'<button class="btn kbtn tableDelbtn otherBtn">添加</button>'+
		case 'role_type':
			var html = '<div>'+
			'<a href="javascript:;" title="添加角色" class="otherBtn">'+
			'<i class="icon icon-hand-right"></i>'+
			'</a>'+
			'</div>';
		return html;
		case 'state':
			var dis = text=="1"?"启用":"禁用";
			var color = text=="1"?"green":"red";
			return '<span style="color:'+color+'">'+dis+'</span>';
		}
	},
	onAfterReady:function(){
		$(".otherBtn").click(function(e){
			e.stopPropagation();
			e.preventDefault();
			var num = $(this).parents('tr').index();
			var record = roleXDatatable.records[num].data;
			var span = $(".rolesShowDiv p[name='"+record.role_id+"']");
			if(span == undefined || span == null || span.length == 0){
				//添加至div显示
				var html = '<p class="itemDiv" name="'+record.role_id+'">'+
				'<input style="display:none" value="'+record.role_id+'">'+
				'<span class="roleshow label label-warning">'+record.role_name+
					'<a href="javascript:;" title="删除" onclick="RoleGroupPage.removeRole(\''+record.role_id+'\')">'+
					'<i class="icon icon-times"></i></a></span>'+
				'</p>';
				$(".rolesShowDiv").append(html);
			}
		});
	},
	// gruopInfoXDataTable的onRenderItem方法
	onRenderGroupItem:function(colName,text){
		switch(colName){
		case 'roleGroup_operation':
			var html='<div class="tableRowsIcon">'+
						'<span title="修改"><a class="editABtn" href="javascript:;">'+
							'<i class="icon icon-edit"></i>'+
						'</a></span>'+
						'<span title="删除"><a class="delABtn" href="javascript:;">'+
						'<i class="icon icon-trash"></i>'+
					'</a></span>'+
					'</div>';
			return html;
		case 'status':
			var dis = text=="1"?"启用":"禁用";
			var color = text=="1"?"green":"red";
			return '<span style="color:'+color+'">'+dis+'</span>';
		case 'flag':
			var $flag = $("<span></span>");
			text = text == "1" ? '是' : "否";
			$flag.text(text);
			return $flag;
		}
	},
	// gruopInfoXDataTable的onAfterReady方法
	onGroupInfoDatatableAfterReady:function(){
		$(".editABtn").click(function(e){
			e.stopPropagation();
			e.preventDefault();
			$("#roleGroupEditDialog .modal-header .modal-title").html("资源组信息修改");
			var num = $(this).parents('tr').index();
			var record = gruopInfoXDataTable.records[num].data;
			$("#rolegroupBaseInfoFrm").autofill(record);
			// $(".inputC").val(record.status);
			$("#searchRole").val("");
			$(".rolesShowDiv").html("");
			if (record.flag == '0') {
				findRoleGroupHasRolesAjax.addParam("groupId",record.group_id);
				findRoleGroupHasRolesAjax.send();
			}
			RoleGroupPage.manageRoleManagementPart(record.flag);
			MsgBox.trigger("", "", "#roleGroupEditDialog");
		});
		$(".delABtn").click(function(e){
			e.stopPropagation();
			e.preventDefault();
			var num = $(this).parents('tr').index();
			var record = gruopInfoXDataTable.records[num].data;
			MsgBox.confirm("确认删除资源组【"+record.group_name+"】？","删除确认",function(){
				var arr = new Array();
		    	arr.push(record.group_id);
			    deleteGroupsAjax.addParam("groupIds",$.toJSON(arr));
			    deleteGroupsAjax.send();
			},null);
		});
	},
	// 查询角色信息
	onFindRolesSucc:function(data){
		
		if(data.success == true){
			var oldRoles = data.info;
			var html = "";
			$.each(oldRoles,function(index,item){
				if(item != null){
					html += '<p class="itemDiv" name="'+item.role_id+'">'+
							'<input style="display:none" value="'+item.role_id+'">'+
							'<span class="roleshow label label-warning">'+item.role_name+
								'<a href="javascript:;" title="删除" onclick="RoleGroupPage.removeRole(\''+item.role_id+'\')">'+
								'<i class="icon icon-times"></i></a></span>'+
						'</p>';
				}
			});
			$(".rolesShowDiv").empty();
			$(".rolesShowDiv").append(html);
			$("#roleOldInfoFrm").submit();
		}
	},
	// 资源组新增
	addRoleGroup:function(){
		$("#rolegroupBaseInfoFrm")[0].reset();
		this.manageRoleManagementPart(0);
		$(".rolesShowDiv").html("");
		$("#searchRole").val("");
		$("#roleGroupEditDialog .modal-header .modal-title").html("资源组信息新增");
		$("#roleOldInfoFrm").submit();
		MsgBox.trigger("", "", "#roleGroupEditDialog");
	},
	refreshTable:function(){
		gruopInfoXDataTable.refresh();
	},
	// 表单查询角色组
	query:function(){
		queryRoleGroupListAjax.send();
	},
	// 删除角色
	removeRole:function(id){
		$(".rolesShowDiv p[name='"+id+"'").remove();
	},
	// 删除成功
	onDeleteInfoSucc:function(data){
		MsgBox.info('删除成功',null,function(){
			gruopInfoXDataTable.refresh();
		});
	},
	// 批量删除角色组
	deleteGroups:function(){
		var selectRecords = gruopInfoXDataTable.getSelectedRecords();
		if(selectRecords == null || selectRecords.length == 0){
			MsgBox.warn("请至少选择一条记录","消息提示",null);
		}else{
		    MsgBox.confirm("确认删除所选【"+selectRecords.length+"】条角色组信息？","删除确认",function(){
		    	var arr = new Array();
		    	var i;
		    	for(i=0;i<selectRecords.length;i++){
		    		arr.push(selectRecords[i].data.group_id);
		    	}
			    deleteGroupsAjax.addParam("groupIds",$.toJSON(arr));
			    deleteGroupsAjax.send();
		    },null);
		}
	},
	// 根据角色名称搜索角色
	searchByroleName:function(){
		roleManaXPagination.resetPagination();
		queryRoleInfoListAjax.send();
	}
};
